@echo off
REM $Id$
REM Installer stub for windows.

REM ======================================
REM
REM set name=oversight
REM set cmd=oversight-installer.cgi
REM set arg=check

set appname=%1
set zipname=%2
set startcmd=%3
set arg=%4
set zipurl=%5
set starturl=%6
REM ======================================

set VERSION=20090117-1
set stub=installer-stub.cgi
echo DOS %appname% Installer 
echo -
echo - mini NMT installer
echo -
echo - Press any key to continue installing [ %appname% ] OR press CTRL-C to abort.
echo -
pause

echo Enter Target NMT Players IP Address:
set /p nmtip=   IP Address:
echo -
set password=1234
echo Enter your NMTs ftp password. Press [ENTER] if it has not been changed.
set /p password=   Ftp Password:

..\helpers\wget -q -O - http://%nmtip%:8883/maintenance.html | find "-402"
if errorlevel 1 goto nmt100403
goto nmt100

:nmt100403
..\helpers\wget -q -O - http://%nmtip%:8883/maintenance.html | find "-403"
if errorlevel 1 goto nmt200

:nmt100
echo NMT 100 Series
set ftpuser=ftpuser
set urldrive=/HARD_DISK
set ftpdrive=/
goto checkftp

:nmt200
echo NMT 200 Series
set ftpuser=nmt

:checkftp

..\helpers\wget -q ftp://%ftpuser%:%password%@%nmtip%
if errorlevel 1 goto badftp
goto getzip

:badftp

echo ERORR Unable to connect to %nmtip% ftp server using user %ftpuser%
goto finish

:getzip
echo -
echo --------------------------------------
echo -

echo - Downloading latest version of %appname% from %zipurl%
if exist %zipname%.zip del %zipname%.zip

..\helpers\wget -O %zipname%.zip %zipurl% 

if exist %zipname%.zip goto install
echo ERROR couldnt get %zipurl%
goto finish

:install
echo on
..\helpers\wget -q -O - http://%nmtip%:8883/ --header="Host: localhost.drives:8883" | find "USB_DRIVE"
if errorlevel 1 goto sata_check
set urldrive=/USB_DRIVE
set ftpdrive=%drive%
goto install2

:sata_check
..\helpers\wget -q -O - http://%nmtip%:8883/ --header="Host: localhost.drives:8883" | find "SATA_DISK"
if errorlevel 1 goto install2
set urldrive=/SATA_DISK
set ftpdrive=%drive%

:install2
echo - %urldrive% %ftpdrive%
echo --------------------------------------
echo -
echo - Converting to tar file
REM Some NMT dont have unzip . convert to tar 

set T=delete-me-%zipname%-delete-me
if exist %T% rmdir /s/q %T%
mkdir %T%

cd %T%
REM busybox doesnt like 7z tar files (use cygwin)
REM ..\..\helpers\7z -tzip x ..\%zipname%.zip
REM ..\..\helpers\7z -ttar a ..\%zipname%.tar
..\..\helpers\unzip -qo ..\%zipname%.zip
if exist %zipname%.tar goto csitar

REM Make the tar file
..\..\helpers\tar cf ..\%zipname%.tar .
goto cleantardir

REM Copy the CSI tar file up
rename %zipname%.tar ..

:cleantardir
cd ..
rmdir /s/q %T%
if exist %zipname%.tar goto install2
echo couldnt convert or extract %zipname%.zip to tar file
goto finish

:install2
del %zipname%.zip
echo -
echo --------------------------------------
echo -

echo - Copying [ %zipname%.tar ] to PCH. 

echo %ftpuser%> ftp.in
echo %password%>> ftp.in
echo bin>> ftp.in
echo cd %ftpdrive%>> ftp.in
echo put ..\helpers\%stub%>> ftp.in
echo put %zipname%.tar>> ftp.in
echo quote site chmod 775 %stub%>> ftp.in
echo quit>> ftp.in

ftp -v -s:ftp.in %nmtip% >> install.log
del %zipname%.tar ftp.in


echo -
echo --------------------------------------
echo -
echo - Running installer

..\helpers\wget -q -O - --header="Host: localhost.drives:8883" http://%nmtip%:8883%urldrive%/%stub%^?%appname%^&%zipname%^&%startcmd%^&%arg% 
echo --------------------------------------
echo ^<body^> >index.html
echo ^<h2^>TV Access^</h2^> >> index.html
echo ^<p^>To use the application through your television go to the web services menu on the TV screen. >> index.html
echo ^<h2^>PC Access^</h2^> >> index.html
echo ^<p^>To use on the PC go to ^<a href=http://%nmtip%:8883/%starturl%^>http://%nmtip%:8883/%starturl%^</a^>. >> index.html
echo ^</body^> >>index.html

echo - End
index.html

:finish
pause
